﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using BestBuy.Common;

namespace BestBuy.DAL
{
    public class CCongTy
    {
        private SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["TradeKeyConnectionString"].ConnectionString);
        private TradeKeyDataContext content = new TradeKeyDataContext();
        public CCongTy() { }

        public int InsertCompany(CongTy ct) {
            CongTy congty = ct;
            try
            {
                content.CongTies.InsertOnSubmit(congty);
                content.SubmitChanges();
            }
            catch {
                return -1;
            }
            return congty.IDCongTy;
        }

        public static bool HasChildren(int parentID)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            int result = context.DanhMucs.Where(u => u.IDDanhMucCha == parentID).Count();
            if (result > 0)
                return true;
            return false;
        }

        public static List<DanhMuc> GetChildrenOfCategory(int? parentID)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            List<DanhMuc> list = new List<DanhMuc>();

            if (parentID.HasValue)
            {
                list = context.DanhMucs.Where(p => p.IDDanhMucCha == parentID).ToList();
            }
            else
            {
                list = context.DanhMucs.Where(p => !p.IDDanhMucCha.HasValue).ToList();
            }

            return list;
        }

        public static DanhMuc GetCategoryByID(int ID)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucs.Where(u => u.IDDanhMuc == ID).FirstOrDefault();
        }

        public static List<DanhMuc> getParentCategory()
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucs.Where(p => p.IDDanhMucCha == null).ToList();
        }

        public static int getIdCompany(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.ThongTinTaiKhoans.Where(p => p.IDTaiKhoan == id).FirstOrDefault().IDCongTy.Value;
        }

        public static CongTy getInfo(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.CongTies.Where(p => p.IDCongTy == id).FirstOrDefault();
        }

        public static List<DanhMucCongTy> getAllCategoryCompany(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucCongTies.Where(p => p.IDCongTy == id).ToList();
        }

        public static DanhMuc getItem(int id)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucs.Where(p => p.IDDanhMuc == id).FirstOrDefault();
        }

        public static void UpdateComnpany(CongTy ct, int idcompany)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            var cty = context.CongTies.Where(p => p.IDCongTy == idcompany).FirstOrDefault();
            cty.TenCongTy = ct.TenCongTy;
            cty.LoaiCongTy = ct.LoaiCongTy;
            cty.ThiTruongChinh = ct.ThiTruongChinh;
            cty.IDNganh = ct.IDNganh;
            cty.TuKhoaSanPham = ct.TuKhoaSanPham;
            cty.SoLuongNhanVien = ct.SoLuongNhanVien;
            cty.NamThanhLap = ct.NamThanhLap;
            cty.GioiThieuCongTy = ct.GioiThieuCongTy;
            cty.TrangCongTy = ct.TrangCongTy;
            cty.LogoCongTy = ct.LogoCongTy;
            cty.TaiLieu = ct.TaiLieu;
            cty.KhuVucNhaMay = ct.KhuVucNhaMay;
            cty.KichThuocNhaMay = ct.KichThuocNhaMay;
            cty.TongSoMuaHangNam = ct.TongSoMuaHangNam;
            cty.SoLuongNhanVienQC = ct.SoLuongNhanVienQC;
            cty.SoLuongNhanVienRD = ct.SoLuongNhanVienRD;
            cty.GiayChungNhan = ct.GiayChungNhan;
            cty.HopDongSanXuat = ct.HopDongSanXuat;
            cty.VonDieuLe = ct.VonDieuLe;
            cty.LoaiQuyenSoHuu = ct.LoaiQuyenSoHuu;
            cty.NguoiDaiDien = ct.NguoiDaiDien;
            cty.TongSoBanHangNam = ct.TongSoBanHangNam;
            cty.TyLeXuatKhau = ct.TyLeXuatKhau;
            context.SubmitChanges();
        }

        public int slTrangDuLieuOfIDCatalogy(int soLuongTrenTrang, int soLuong)
        {
            if (soLuong > 0)
            {
                int soTrang = soLuong / soLuongTrenTrang;
                if (soLuong % soLuongTrenTrang != 0)
                {
                    soTrang++;
                }

                return soTrang;
            }
            return -1;
        }

        public List<int> FilterMaDanhMucOfIDCatalogy(List<DanhMuc> list, int iddanhmuc)
        {
            DataTable dt = new DataTable();
            List<int> arrMaDuLieu = new List<int>();
            if (list.Count > 0)
            {
                var q = (from dl in content.DanhMucCongTies
                         where list.Contains(dl.DanhMuc)
                         select dl.IDCongTy).ToList();
                arrMaDuLieu = q;
            }
            else
            {
                string sql = "select dl.IDCongTy from DanhMucCongTy dl where dl.IDDanhMuc = " + iddanhmuc.ToString();
                sqlCon.Open();
                SqlCommand cmd = new SqlCommand(sql, sqlCon);
                SqlDataReader dr = cmd.ExecuteReader();
                arrMaDuLieu.Add(int.Parse(iddanhmuc.ToString()));
                while (dr.Read())
                {
                    arrMaDuLieu.Add(int.Parse(dr["IDCongTy"].ToString()));
                }

                sqlCon.Close();
            }

            return arrMaDuLieu;
        }

        public List<CongTy> GetListOfIDCatalogy(List<DanhMuc> list, int iddanhmuc, int begin, int size)
        {
            List<int> arrMaDuLieu = this.FilterMaDanhMucOfIDCatalogy(list, iddanhmuc);
            var result = (from dl in content.CongTies
                          where arrMaDuLieu.Contains(dl.IDCongTy) && dl.TinhTrang == (int)SanPhamStatus.Active
                          orderby dl.IDCongTy descending
                          select dl).Skip(begin).Take(size).ToList();
            return result;
        }

        public DanhMuc GetCatalogyOfIDCompany(int id)
        {
            var q = (from dl in content.DanhMucCongTies
                     where dl.IDCongTy == id
                     select dl).FirstOrDefault();
            DanhMuc dm = GetCatalogyOfID(q.IDDanhMuc);
            return dm;
        }

        private DanhMuc GetCatalogyOfID(int iddm)
        {
            var q = (from dl in content.DanhMucs
                     where dl.IDDanhMuc == iddm
                     select dl).FirstOrDefault();
            if (q.IDDanhMucCha != null)
            {
                q = GetCatalogyOfID(q.IDDanhMucCha.Value);
            }
            return q;
        }

        public List<CongTy> Search(string strSearch, int IDCountry, int begin, int size)
        {
            List<CongTy> list = null;
            list = (from dl in content.CongTies
                    where dl.TinhTrang == (int)SanPhamStatus.Active &&
                      (dl.TenCongTy.Contains(strSearch) || dl.LoaiCongTy.Contains(strSearch)
                      || dl.TuKhoaSanPham.Contains(strSearch) || dl.GioiThieuCongTy.Contains(strSearch))
                    select dl).Skip(begin).Take(size).ToList();
            if (IDCountry > 0) // search theo ID Quoc Gia
            {
                list = list.Where(u => u.ThongTinTaiKhoans.Where(p => p.IDQuocGia == IDCountry).Select(p => p.IDQuocGia).Contains(IDCountry)).ToList();
            }
            return list;
        }
    }
}